package com.example.dangerouscargodriver.ui.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.baiju.netmanager.Http.IHttpRequest;
import com.example.dangerouscargodriver.adapter.EmployeeInfoAdapter;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.controller.UserInfoController;
import com.example.dangerouscargodriver.entry.CompanyInfo;
import com.example.dangerouscargodriver.entry.EmployeeInfo;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.example.dangerouscargodriver.widget.ActionBar;
import com.example.dangerouscargodriver.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

public class EmployeeManagerActivity extends HttpRequestActivity implements View.OnClickListener {

    private static final String TAG = "EmployeeManagerActivity";
    private ActionBar mActionBar = null;
    private Button mAddEmployee = null;

    private EmployeeInfoAdapter mAdapterNormalEmployee = null;
    private TextView mEmployeeManager = null;

    private List<EmployeeInfo> mEmployeeInfo = null;

    private boolean mIsCreate = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.initHttpComponent(EmployeeManagerActivity.class);
        setContentView(R.layout.activity_employee_manager);

        mAddEmployee = findViewById(R.id.btn_add_employee);
        mAddEmployee.setOnClickListener(this);
        mEmployeeManager = findViewById(R.id.tv_manager_name);

        mActionBar = findViewById(R.id.actionBarEmployeeManager);
        mActionBar.setOnBackClickListener( new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        initEmployeeManagerInfo();
        initNormalEmployeeInfo();
    }

    private void initNormalEmployeeInfo() {
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.layout_normal_employee_List);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        mAdapterNormalEmployee = new EmployeeInfoAdapter();
        recyclerView.setAdapter(mAdapterNormalEmployee);
    }

    private void initEmployeeManagerInfo() {
        mEmployeeManager.setText(UserInfoController.Instance().getUserInfo().getMobilephone());
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_add_employee){
            Intent intent = new Intent(this, EmployeeAddActivity.class);
            startActivity(intent);
        }
        else{

        }
    }

    @Override
    public void onResume(){
        super.onResume();
        executeSearchEmployeeInfoAction();
    }

    private void executeSearchEmployeeInfoAction(){
        HashMap<String, String> header = new HashMap<String, String>();
        header.put("token", AuthController.Instance().getToken());

        sendHttpRequest(
                IHttpRequest.RequestMethod.GET_METHOD,
                RemoteAPI.REQUEST_V1_CHILD_ACCOUNT,
                header,
                null,
                RemoteAPICmd.REQUEST_V1_CHILD_ACCOUNT);
    }

    @Override
    protected void onHttpResponse(int actionId, String response) {
        try {
            JSONObject res = new JSONObject(response);
            Gson gson = new Gson();
            if(actionId == RemoteAPICmd.REQUEST_V1_CHILD_ACCOUNT){
                Log.d(TAG, "onResponse: " + response);
                ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                if(responseInfo != null && responseInfo.getStatus() == 1){
                    Type type = new TypeToken<List<EmployeeInfo>>() {}.getType();
                    mEmployeeInfo = gson.fromJson(res.getJSONObject("data").getJSONArray("list").toString(), type);
                    mAdapterNormalEmployee.clearData();
                    mAdapterNormalEmployee.appendData(mEmployeeInfo);
                }
                else{

                }
            }
            else{

            }
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onHttpErrorResponse(int actionId, String response) {
        if(actionId == RemoteAPICmd.REQUEST_V1_CHILD_ACCOUNT){
            noticeOffline();
        }
    }
}